package com.ruoyi.web.pojo.vo.warning;

import com.ruoyi.web.domain.warning.WarningFactorRecord;
import com.ruoyi.web.domain.warning.WarningInsectRecord;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @desc  某一天的预警数量统计
 * @author fwx 2024/2/18 14:02
 **/
@Data
public class WarningRecordCountByDayVO {

    @ApiModelProperty(value = "日期")
    public String dateStr;

    @ApiModelProperty(value = "预警数量")
    public Integer warningNum;

    @ApiModelProperty(value = "虫情预警详情")
    public List<WarningInsectRecord> insectRecords;

    @ApiModelProperty(value = "灾情预警详情")
    public List<WarningFactorRecord> factorRecords;

    /**
     * @desc  合并两个集合的数据
     * @author fwx 2024/2/18 14:45
     **/
    public static List<WarningRecordCountByDayVO> addCountList(List<WarningRecordCountByDayVO> list1 , List<WarningRecordCountByDayVO> list2){

        Map<String,WarningRecordCountByDayVO> voMap = new HashMap<>();

        for (WarningRecordCountByDayVO vo : list1) {
            voMap.put(vo.getDateStr(),vo);
        }

        for (WarningRecordCountByDayVO vo : list2) {
            WarningRecordCountByDayVO item = voMap.get(vo.getDateStr());

            if(item != null){
                vo.addCount(item);
                if(item.getFactorRecords() != null){
                    vo.setFactorRecords(item.getFactorRecords());
                }

                if(item.getInsectRecords() != null){
                    vo.setInsectRecords(item.getInsectRecords());
                }
            }

            voMap.put(vo.getDateStr(),vo);
        }

        return voMap.values().stream().sorted(Comparator.comparing(WarningRecordCountByDayVO::getDateStr)).collect(Collectors.toList());
    }

    public void addCount(WarningRecordCountByDayVO vo){
        this.warningNum += vo.getWarningNum();
    }
}
